# utils.py: Utility functions for fonr
import re
from fonr.googlevoice import Voice

def check_phone_number(raw_number):
  """Number is a string the form of XXXXXXXXXX.  Checks if its a valid US
  phone number"""
  # Fix number to match format
  r = re.compile('^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$')
  match = r.match(raw_number)
  if not match:
    return (False, 'Invalid phone number: Unrecognized format')
  number = ''.join(match.groups())
  # Check if its 10 numbers
  match = re.match('([0-9]{10})', number)
  if not match:
    return (False, '%s is not 10 digits of numbers')
  # Check area code
  match = re.match('[2-9][0-8][0-9][0-9]{7}', number)
  if not match:
    return (False, 'Invalid area code %s' % number[0:3])
  # Check exchange code
  match = re.match('[0-9]{3}[2-9][0-9]{2}[0-9]{4}', number)
  if not match:
    return (False, 'Invalid exchange code %s' % number[3:6])
  return (True, number)
  
# Use Google voice to send a text message
def send_text(number, message):
  voice = Voice()
  voice.login()
  voice.send_sms(number, message)